package com.yuelao.hbase;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

import static org.apache.hadoop.hbase.util.Bytes.toBytes;

/**
 * Description
 *
 * @author fengliye
 * @version v1.0
 * @date 28/12/2018 14:29
 * @modify detail
 * 版本       修改人        修改时间      修改内容描述
 * ---------------------------------------------
 * <p>
 * ---------------------------------------------
 */
public class HBaseDemo {
    public static void main(String[] args) {
        //testGet();
        testScan();
    }

    public static void testGet() {
        String TABLE_NAME = "student";
        try {
            HTable table = (HTable) HBaseClient.getConn().getTable(TableName.valueOf(TABLE_NAME));
            Get get = new Get(toBytes("2"));
            Result result = table.get(get);
            byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
            System.out.println(Bytes.toString(value));


        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testScan() {
        String TABLE_NAME = "student";
        try {
            HTable table = (HTable) HBaseClient.getConn().getTable(TableName.valueOf(TABLE_NAME));
            // Instantiating the Scan class
            Scan scan = new Scan();
            scan.withStartRow(toBytes("10"));
            //scan.withStopRow(toBytes("6"));
            //scan.setCaching(5);
            scan.setLimit(5);

            // Getting the scan result
            ResultScanner scanner = table.getScanner(scan);
            int index = 0;
            for (Result result : scanner) {
                System.out.println("rowKey: " + Bytes.toString(result.getRow()));
                byte[] value = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
                System.out.println(Bytes.toString(value));
                index ++;
            }
            System.out.println("size:" + index);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


